package org.linlinjava.litemall.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.usermodel.CellStyle;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallBookList;
import org.linlinjava.litemall.db.domain.LitemallGoodsProduct;
import org.linlinjava.litemall.db.service.LitemallBookBoxService;
import org.linlinjava.litemall.db.service.LitemallBookListService;
import org.linlinjava.litemall.db.service.LitemallGoodsProductService;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.*;

/**
 * 专题服务
 */
@RestController
@RequestMapping("/wx/bookList")
@Validated
public class WxBookListController {
    private final Log logger = LogFactory.getLog(WxBookListController.class);

    @Autowired
    private LitemallBookListService bookListService;
    @Autowired
    private LitemallGoodsService goodsService;
    @Autowired
    private LitemallGoodsProductService goodsProductService;
    @Autowired
    private LitemallBookBoxService bookBoxService;

    /**
     * 书单列表
     *
     * @param page  分页页数
     * @param limit 分页大小
     * @return 书单列表
     */
    @GetMapping("list")
    public Object list(@RequestParam Integer type,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallBookList> bookListList = bookListService.queryList(type, page, limit, sort, order);
        return ResponseUtil.okList(bookListList);
    }

    /**
     * 书单详情
     *
     * @param id 专题ID
     * @return 书单详情
     */
    @GetMapping("/detail")
    public Object detail(@LoginUser Integer userId, @RequestParam Integer id) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        LitemallBookList bookList = bookListService.findById(id);
        Integer[] listGoods = bookList.getGoods();

        Integer type = bookList.getType();
        if (listGoods == null || listGoods.length == 0) {
            return ResponseUtil.ok(null);
        }
        List<Map<String, Object>> goods = new ArrayList<>();
        BigDecimal memberPrice = BigDecimal.ZERO;
        if (type == 1) {
            for (Integer i : listGoods) {
                Map<String, Object> goodsMap = new HashMap<>();
                LitemallGoodsProduct products = goodsProductService.queryByGidAndSpecification(i, "旧");
                LitemallGoods good = goodsService.findByIdVO(i);
                if (good != null && products != null && bookBoxService.findByUserIdAndGoodsProductId(userId, products.getId()) == null) {
                    memberPrice = memberPrice.add(products.getMemberPrice());
                    goodsMap.put("goods", good);
                    goodsMap.put("productId", products.getId());
                    goodsMap.put("joined",false);
                }
                else if (good != null && products != null && bookBoxService.findByUserIdAndGoodsProductId(userId, products.getId()) != null) {
                    memberPrice = memberPrice.add(products.getMemberPrice());
                    goodsMap.put("goods", good);
                    goodsMap.put("productId", products.getId());
                    goodsMap.put("joined",true);
                }
                if (!goodsMap.isEmpty()) {
                    goods.add(goodsMap);
                }
            }

        } else {
            for (Integer i : listGoods) {
                Map<String, Object> goodsMap = new HashMap<>();
                LitemallGoods good = goodsService.findByIdVO(i);
                if (good != null) {
                    goodsMap.put("goods", good);
                    memberPrice = memberPrice.add(good.getMemberPrice());
                }
                if (!goodsMap.isEmpty()) {
                    goods.add(goodsMap);
                }
            }
        }

        Map<String, Object> entity = new HashMap<>();
        entity.put("bookList", bookList);
        entity.put("goods", goods);
        entity.put("memberPrice", memberPrice);
        return ResponseUtil.ok(entity);
    }

    /**
     * 相关书单
     *
     * @param id 专题ID
     * @return 相关书单
     */
    @GetMapping("related")
    public Object related(@NotNull Integer type, Integer id) {
        List<LitemallBookList> bookListRelatedList = bookListService.queryRelatedList(type, id, 0, 4);
        return ResponseUtil.okList(bookListRelatedList);
    }
}